//
//  CityTableViewController.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/11.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

class CityTableViewController: UITableViewController {

    
    var ID:String?
    var titleName:String?
    var page:Int?
   
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置navigation
        self.setNavigation()
        //获得数据
        self.getData()
        
        self.tableView.registerNib(UINib(nibName: "WordTrabelTVCell", bundle: nil), forCellReuseIdentifier: "cell")
        
        //MJ刷新
        //顶部刷新
        let header = MJRefreshNormalHeader()
        //底部刷新
        let footer = MJRefreshAutoNormalFooter()
        header.setRefreshingTarget(self, refreshingAction: Selector("headerRefresh"))
        self.tableView.mj_header = header
        
        //上拉刷新
        footer.setRefreshingTarget(self, refreshingAction: Selector("footerRefresh"))
        self.tableView.mj_footer = footer
        
        
    }
    
    //下拉刷新
    func headerRefresh() {
        print("下拉刷新")
        self.getData()
        //结束刷新
        self.tableView.mj_header.endRefreshing()
    }
    
    //底部刷新
    func footerRefresh() {
        print("上拉刷新")
        self.getMoreData()
        self.tableView.mj_footer.endRefreshing()
    }
    
    //设置navigation
    func setNavigation() {
        self.navigationItem.title = self.titleName! + "热门城市"
        self.navigationController?.navigationBar.barTintColor = UIColor.lightGrayColor()
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "HTiconfont-fanhui")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("leftBtnAction"))
    }
    
    //返回按钮的事件
    func leftBtnAction() {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    //数据解析
    func getData() {
        self.page = 1
        let pageString = String(self.page)
        let urlString = "http://www.koubeilvxing.com/places?countryId=" + self.ID! + "page=" + pageString + "&rows=10"
        SHHGetData.sharedGetData.requestCityModelWithUrlString(urlString) { (array:Array<CityModel>) -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.tableView.reloadData()
            })
        }
    }
    
    //下拉刷新的时候加载数据
    func getMoreData() {
        self.page!++
        let pageString = String(self.page)
        let urlString = "http://www.koubeilvxing.com/places?countryId=" + self.ID! + "page=" + pageString + "&rows=10"
        SHHGetData.sharedGetData.requestCityModelWithUrlString(urlString) { (array:Array<CityModel>) -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.tableView.reloadData()
            })
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return SHHGetData.sharedGetData.cityArray.count
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return (UIScreen.mainScreen().bounds.size.height) / 3.5
        
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? WordTrabelTVCell

        let model = SHHGetData.sharedGetData.cityArray[indexPath.row]
        cell?.imv.sd_setImageWithURL(NSURL(string: "http://img.koubeilvxing.com/pics" + model.path!))
        let string:String = model.name_cn! + "(\(model.name!))"
        cell?.label?.text = string
        
        return cell!
    }

    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let cityVC = CityDetailVC()
        //传值
        let model = SHHGetData.sharedGetData.cityArray[indexPath.row]
        cityVC.initWithPlaceID(model.ID!)
        cityVC.headerName = model.name_cn!
        
        self.navigationController?.pushViewController(cityVC, animated: true)
    }

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
